package com.chao.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public final class AddressConstant {

    public static JSONArray region = new JSONArray();

    public static JSONObject address = new JSONObject();
    public static JSONArray address_province_city = new JSONArray();

    public static void setAddress(JSONObject address){
        AddressConstant.address = address;
        setRegion(address.getJSONArray("data"),"");
        setRegionProvinceCity(address.getJSONArray("data"));
    }

    public static void setRegion(JSONArray address, String up){
        address.forEach(o -> {
            JSONObject json = (JSONObject) JSONObject.toJSON(o);
            json.remove("areacode"); //,json.getString("label"));
            AddressConstant.region.add(up + json.getString("label"));
            if(json.containsKey("children")){
                setRegion(json.getJSONArray("children"),up + json.getString("label"));
            }
        });
    }

    public static void setRegionProvinceCity(JSONArray data){
        data.forEach(o -> {
            JSONObject json = (JSONObject) JSONObject.toJSON(o);
            JSONObject tmp = new JSONObject();
            JSONArray children = new JSONArray();
            json.getJSONArray("children").forEach(v -> {
                JSONObject json1 = (JSONObject) JSONObject.toJSON(v);
                JSONObject tmp1 = new JSONObject();
                tmp1.put("label", json1.getString("label"));
                children.add(tmp1);
            });
            tmp.put("label", json.getString("label"));
            tmp.put("children", children);
            address_province_city.add(tmp);
        });
    }

    

}
